$(function () {

    var role_datagrid = $("#role_datagrid");
    var role_dialog = $("#role_dialog");
    var role_form = $("#role_form");
    var selectPermission = $("#selectPermission");

    /*easyui的数据列表组件初始化*/
    role_datagrid.datagrid({
        title:"角色管理",
        url: "/role/query.do",
        fit: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        sortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: "#role_toolbar",
        columns: [[
            {field: 'id', title: 'id', width: 100, checkbox: true},
            {field: 'name', title: '角色名称', width: 100},
            {field: 'sn', title: '角色编号', width: 100},
        ]]
    })


    /*弹出编辑框的初始化*/
    role_dialog.dialog({
        width: 550,
        height: 400,
        closed: true,
        buttons: [{
            text: "保存",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                methodObj["save"]();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["cancel"]();
            }
        }],
        onClose: function () {
            /*清空普通的数据*/
            role_form.form('clear');

            /*重置权限dataGrid组件中的数据*/
            selectPermission.datagrid('loadData', []);//清空
            $("#allPermission").datagrid('load');//重新加载
        }
    })

    /*编辑框中的权限分配数据表初始化*/
    $("#allPermission").datagrid({
        title: "所有权限",
        height: 200,
        url: "/permission/query.do",
        fitColumns: true,
        onDblClickRow: function (index, row) {
            selectPermission.datagrid('appendRow', row);
            $("#allPermission").datagrid('deleteRow', index);
        },
        columns: [[
            {field: 'name', title: '权限名称', width: 50, align: 'center'}
        ]]
    })

    selectPermission.datagrid({
        title: "已有权限",
        height: 200,
        url: "/permission/rolePermisson.do",
        fitColumns: true,
        onDblClickRow: function (index, row) {
            $("#allPermission").datagrid('appendRow', row);
            selectPermission.datagrid('deleteRow', index);
        },
        /*数据加载完毕,消重*/
        onLoadSuccess: function (data) {
            var ids = $.map(data.rows, function (ele) {
                return ele.id;
            });
            var ps = $("#allPermission").datagrid('getRows');
            for (var i = ps.length - 1; i >= 0; i--) {
                var permission = ps[i];
                var index = $.inArray(permission.id, ids);
                if (index >= 0) {
                    $("#allPermission").datagrid('deleteRow', i);
                }
            }
        },
        columns: [[
            {field: 'name', title: '权限名称', width: 50, align: 'center'}
        ]]
    })

    /*菜单复选框优化*/
    $("#menu_combotree").combotree({
        width:170,
        onCheck:function(node){
            /!*获取树型对象*!/
            var treeObj= $("#menu_combotree").combotree('tree');
            /!*获取父节点*!/
            var parent = treeObj.tree('getParent',node.target);
            if (parent) {
                /!*勾选父节点*!/
                treeObj.tree('check',parent.target);
            }
        }
    });

    /*事件对象*/
    var methodObj = {
        reload: function () {
            role_datagrid.datagrid("reload");
        },

        /*新增角色按钮事件*/
        add: function () {
            role_dialog.dialog('setTitle', "新增角色");
            role_dialog.dialog('open');
        },

        /*编辑角色按钮事件*/
        edit: function () {
            role_dialog.dialog('setTitle', "编辑");
            /*c查看是否有选中的角色*/
            var row = role_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'error');
                return;
            }
            /*基本角色数据回显*/
            role_form.form('load', row);

            /*权限回显,发角色id查中间表中的权限id,就行了*/
            selectPermission.datagrid('load', {roleId: row.id});

            /*菜单回显*/
            $.get("/menu/roleMenu.do",{id:row.id},function (data) {
                $("#menu_combotree").combotree('setValues',data);
            })
            /*打开弹出框*/
            role_dialog.dialog('open');
        },

        /*表单保存事件*/
        save: function () {
            $.messager.confirm("温馨提示", "确定要保存吗?", function () {
                role_form.form('submit', {
                    url: "/role/saveOrUpdate.do",
                    /*表单提交前的发额外参数*/
                    onSubmit: function (param) {
                        /*权限*/
                        var rows = selectPermission.datagrid('getRows');
                        for (var i = 0; i < rows.length; i++) {
                            param["permissions[" + i + "].id"] = rows[i].id;
                        }

                        var menus = $("#menu_combotree").combotree('getValues');
                        for (var i = 0; i < menus.length; i++) {
                            param["menus[" + i + "].id"] = menus[i];
                        }
                        /*手动提交表单*/
                        return role_form.form('validate');
                    },
                    success: function (data) {
                        /*easyui的表单响应数据不是json,需要转换*/
                        data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                role_dialog.dialog('close');
                                role_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error")
                        }
                    }
                })
            })
        },

        /*删除角色事件*/
        del: function () {
            /*检查是否有选中的行*/
            var row = role_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("温馨提示", "请选择一行数据", "error");
                return;
            }
            /*删除操作*/
            $.messager.confirm("温馨提示", "确定要删除吗", function (tr) {
                if (tr) {
                    $.post("/role/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                role_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error");
                        }
                    })
                }
            })
        },

        /*表单取消事件*/
        cancel: function () {
            role_dialog.dialog('close');
        }
    }

    /*统一绑定事件*/
    $("a[data-method]").click(function () {
        var method = $(this).data("method");
        methodObj[method]();
    })
})
